/**
 * 
 */
package com.realgodo.site.web;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.web.struts2.Struts2Utils;

import com.realgodo.site.common.Constant;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.service.UserManager;
import com.realgodo.site.service.space.SpaceManager;

@Scope("prototype")
@Results( {
		@Result(name = "user_index", type = "redirect", location = "${base}/user/?n=1"),
		@Result(name = "error", type = "redirect", location = "${base}/login") })
public class LoginController extends ControllerSupport {

	/** */
	private static final long serialVersionUID = 5255926579472286365L;
	private User user;
	@Autowired
	private UserManager userManager;
	@Autowired
	private SpaceManager spaceManager;

	public String index() {
		return "login";
	}

	public String check() {
		user = this.userManager.login(user);
		String pass = "1";
		if (null != user) {
			pass = "2";
		}
		Struts2Utils.renderJson(pass);
		return null;
	}

	public String login() {
		user = this.userManager.login(user);
		if (null != user) {
			Struts2Utils.getSession().setAttribute(Constant.LOGIN_USER_ID,
					user.getId());
			Struts2Utils.getSession().setAttribute(Constant.LOGIN_SPACE,
					spaceManager.getSpaceByUserId(user.getId()));
		}
		return (null != user) ? "user_index" : "error";
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

}
